﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.Data;

using System.Runtime.InteropServices;
using System.Web;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;




namespace DotNetNuke.Modules.UserDefinedTable
{


  public class DataTypeDownload : DataType
  {
    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {
      if (ds != null)
      {
        ArrayList list = new ArrayList();
        DataTable table = ds.Tables[DataSetTableName.Data];
        DotNetNuke.Modules.UserDefinedTable.TokenReplace replace2 = new DotNetNuke.Modules.UserDefinedTable.TokenReplace();
        replace2.ModuleId = ModuleId;
        DotNetNuke.Modules.UserDefinedTable.TokenReplace replace = replace2;

        FieldSettings settings, settings4, settings5;

        foreach(DataRow current in ds.Tables[DataSetTableName.Fields].Rows)
        {
          if ((string)current[FieldsTableColumn.Type] == this.Name)
          {
            settings4 = new FieldSettings();
            settings5 = settings4;
            settings5.Title = (string)current[FieldsTableColumn.Title];
            settings5.OutputFormat = Utilities.CStrN((current[FieldsTableColumn.OutputSettings]), "");
            settings5.Abbreviate = Utilities.CBoolN((current[FieldsTableColumn.NormalizeFlag]), false);
            settings = settings5;
            list.Add(settings);
            table.Columns.Add(new DataColumn(settings.Title + DataTableColumn.Appendix_Url, typeof(string)));
            table.Columns.Add(new DataColumn(settings.Title + DataTableColumn.Appendix_Caption, typeof(string)));
            table.Columns.Add(new DataColumn(settings.Title + DataTableColumn.Appendix_Original, typeof(string)));
          }
        }
        if (list.Count > 0)
        {
          bool flag;
          PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
          Hashtable moduleSettings = new ModuleController().GetModuleSettings(ModuleId);
          if (!moduleSettings.ContainsKey(SettingName.URLNewWindow))
            flag = false;
          
          else
            flag = bool.Parse((string)moduleSettings[SettingName.URLNewWindow]);
          
          FileController controller = new FileController();

          bool flag2;
          string str2, url, strURL, outputFormat, fileName, str6;

          foreach (DataRow row in table.Rows)
          {
            foreach (FieldSettings settings3 in list)
            {
              str2 = string.Empty;
              strURL = (string)row[settings3.Title];
              if (flag)
                flag2 = URLUtil.OpenUrlInNewWindow(ref strURL);

              else
                flag2 = true;

              strURL = URLUtil.StripURL(ref strURL);
              url = "";
              outputFormat = string.Empty;
              if ( !string.IsNullOrEmpty(strURL) )
              {
                url = HttpUtility.HtmlEncode(DotNetNuke.Common.Globals.LinkClick(strURL, currentPortalSettings.ActiveTab.TabID, ModuleId));
                fileName = "";
                str6 = "";

                if (strURL.StartsWith("FileID=", StringComparison.OrdinalIgnoreCase))
                {
                  FileInfo fileById = controller.GetFileById(int.Parse(UrlUtils.GetParameterValue(strURL)), currentPortalSettings.PortalId);
                  if (fileById != null)
                  {
                    fileName = fileById.FileName;
                    if (settings3.Abbreviate)
                      str6 = fileById.Folder + fileName;

                    else
                      str6 = fileName;

                  }
                }
                else
                {
                  fileName = DotNetNuke.Common.Globals.ResolveUrl(url);
                  if (settings3.Abbreviate)
                    str6 = fileName.Substring(fileName.LastIndexOf("/") + 1);

                  else
                    str6 = fileName;

                }
                outputFormat = settings3.OutputFormat;
                if (string.IsNullOrEmpty(outputFormat))
                  outputFormat = fileName;

                else
                  outputFormat = replace.ReplaceEnvironmentTokens(outputFormat, row);

                if (Utilities.CBoolN((moduleSettings[SettingName.ForceDownload]), false))
                  url = url + "&amp;forcedownload=true";

                str2 = string.Format("<!--{0}--><a href=\"{1}\" {2}>{3}</a>", new object[] { str6, url, (flag2 ? " target=\"_blank\"" : "").ToString(), outputFormat });
              }
              row[settings3.Title] = str2;
              row[settings3.Title + DataTableColumn.Appendix_Caption] = outputFormat;
              row[settings3.Title + DataTableColumn.Appendix_Original] = strURL;
              row[settings3.Title + DataTableColumn.Appendix_Url] = url;
            }
          }
        }
      }
    }




    public override string Name
    {
      get
      {
        return "Download";
      }
    }





    public override bool SupportsDefaultValue
    {
      get
      {
        return true;
      }
    }





    public override bool SupportsEditing
    {
      get
      {
        return true;
      }
    }





    public override bool SupportsInputSettings
    {
      get
      {
        return true;
      }
    }





    public override bool SupportsNormalizeFlag
    {
      get
      {
        return true;
      }
    }





    public override bool SupportsOutputSettings
    {
      get
      {
        return true;
      }
    }





    public override bool SupportsSearch
    {
      get
      {
        return true;
      }
    }





    [StructLayout(LayoutKind.Sequential)]
    private struct FieldSettings
    {
      public string Title;
      public string OutputFormat;
      public bool Abbreviate;
    }



  }

}

